package org.easymodelers.gmf.creator.impl;

import java.io.IOException;

import org.easymodelers.modeler.data.ModelerDefinition;
import org.easymodelers.processing.GenModelCreator;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
import org.eclipse.gmf.internal.bridge.transform.TransformOptions;
import org.eclipse.gmf.internal.bridge.transform.TransformToGenModelOperation;

public class DefaultGmfGenModelCreator implements GenModelCreator {

	public static URI getURI(String path) {
        return URI.createPlatformResourceURI(path, true);
	}

	protected static void configureOptions(TransformOptions options) {
        options.setUseRuntimeFigures(true);
        options.setUseMapMode(true);
        options.setGenerateRCP(false);
	}

	public void createGenModel(String modelPath, ModelerDefinition modeler) throws IOException {
		/*GmfModelsUtils.createGmfModel(modelPath, new ModelCreatorCallback<GmfModel>() {
			public GmfModel createModelRootElement() throws IOException {
				return null;
			}
		});*/

		//gmfModel
		//Gmfmo
		
		URI mapModelUri = getURI(modeler.getName() + "gmfmap");
		URI emfGenModelUri = getURI(modeler.getName() + "genmodel");
		URI genModelUri = getURI(modeler.getName() + "gmfgen");
		
		final ResourceSet rs = new ResourceSetImpl();
        TransformToGenModelOperation op = new TransformToGenModelOperation(rs);
        configureOptions(op.getOptions());

		//TransformToGenModelOperation
		
        try {
        	op.loadMappingModel(mapModelUri, new NullProgressMonitor());
        	op.loadGenModel(emfGenModelUri, new NullProgressMonitor());
        	op.setGenURI(genModelUri);
        	IStatus status = op.executeTransformation(new NullProgressMonitor());
        } catch(CoreException ex) {
        	ex.printStackTrace();
        }

        //gmfGenModel.refreshLocal(IResource.DEPTH_ONE, new NullProgressMonitor());
	}

}
